<template>
  <div class="batch-list">
    <div class="search-box">
      <div class="search-box-label">
        <span>商家</span>
        <el-input v-model="shop_name" placeholder="请输入商家名称"></el-input>
      </div>
      <div class="search-box-label">
        <span>关键字</span>
        <el-input v-model="keywords" placeholder="请输入条形码/商品"></el-input>
      </div>
      <div class="search-box-label">
        <span>保质期</span>
        <el-input
          v-model="shelf_life"
          placeholder="请输入保质期"
          style="width: 120px"
        ></el-input>
      </div>
      <div class="search-box-label">
        <span>生产日期</span>
        <el-date-picker
          v-model="production_date"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </div>
      <div class="search-box-label">
        <span>长保/短保</span>
        <el-select
          v-model="good_quality"
          class="radios"
          placeholder="请选择长保/短保"
        >
          <el-option label="长保" :value="1"> </el-option>
          <el-option label="短保" :value="2"> </el-option>
        </el-select>
      </div>
    </div>
    <div class="sear_btns">
      <el-button type="primary" @click="search">查询</el-button>
      <el-button @click="resetSearch">重置</el-button>
      <el-button type="primary" @click="addBatch">增加批次</el-button>
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column label="编号" prop="comm_batch_id"> </el-table-column>
      <el-table-column label="商家名称" prop="shop_name" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="条形码" prop="comm_info_code"> </el-table-column>
      <el-table-column label="商品名称" prop="comm_name"> </el-table-column>
      <el-table-column label="短保/长保" prop="good_quality">
        <template slot-scope="scope">
          {{ saveLong[scope.row.good_quality] }}
        </template>
      </el-table-column>
      <el-table-column label="保质期" prop="shelf_life"> </el-table-column>
      <el-table-column label="生产日期" prop="production_date">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            class="opt_btn"
            type="primary"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            class="opt_btn"
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="currentChange"
      :current-page="page"
      :page-size="size"
      layout="total, prev, pager, next"
      :total="total"
      background
    ></el-pagination>

    <el-dialog
      :title="optType == '1' ? '添加' : '编辑'"
      :visible.sync="dialogEdit"
      width="40%"
      @close="closeModal"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="条形码" prop="code">
          <el-input v-model="ruleForm.code" :disabled="optType == 2"></el-input>
        </el-form-item>
        <el-form-item label="生产日期" prop="production_date">
          <el-date-picker
            style="width: 100%"
            v-model="ruleForm.production_date"
            type="date"
            placeholder="选择生产日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogEdit = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "@/api/batchlist";
export default {
  name: "BatchList",
  data() {
    return {
      shop_name: "",
      keywords: "",
      shelf_life: "",
      production_date: "",
      good_quality: "",
      tableData: [],
      page: 1,
      size: 10,
      total: 0,
      saveLong: {
        1: "长保",
        2: "短保"
      },
      optType: "1", //1.新增 2.编辑
      comm_batch_id: "",
      ruleForm: {
        code: "",
        production_date: ""
      },
      rules: {
        code: [{ required: true, message: "请输入条形码", trigger: "blur" }],
        production_date: [
          { required: true, message: "请选择生产日期", trigger: "blur" }
        ]
      },
      dialogEdit: false
    };
  },
  created() {
    this.getListData();
  },
  methods: {
    getListData() {
      let data = {
        keywords: this.keywords,
        shop_name: this.shop_name,
        shelf_life: this.shelf_life,
        production_date: this.production_date,
        good_quality: this.good_quality, //1长保 2短保 不传,默认 全部
        page: this.page,
        size: this.size
      };
      api.getList(data).then(res => {
        const { list, count } = res.data;
        this.tableData = list;
        this.total = count;
      });
    },
    search() {
      this.page = 1;
      this.getListData();
    },
    handleEdit(row) {
      this.dialogEdit = true;
      this.optType = "2";
      this.ruleForm.code = row.comm_info_code;
      this.ruleForm.production_date = row.production_date;
      this.comm_batch_id = row.comm_batch_id;
    },
    handleDelete(index, row) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          api
            .delBatch({ comm_batch_id_str: row.comm_batch_id })
            .then(res => {
              if (res.code == 200200) {
                this.$message.success(res.msg);
                this.getListData();
              }
            })
            .catch(err => {
              this.$message.error(err.msg);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    addBatch() {
      this.dialogEdit = true;
      this.optType = "1";
      this.ruleForm.code = "";
      this.ruleForm.production_date = "";
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let data = { ...this.ruleForm };
          let fn =
            this.optType == "1"
              ? api.createBatch(data)
              : api.editBatch({ ...data, comm_batch_id: this.comm_batch_id });
          fn.then(res => {
            if (res.code == 200200) {
              this.Message.success(res.msg);
              this.dialogEdit = false;
              this.getListData();
            } else {
              this.Message.error(res.msg);
            }
          }).catch(err => {
            this.Message.error(err.msg);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetSearch() {
      this.keywords = "";
      this.shop_name = "";
      this.shelf_life = "";
      this.production_date = "";
      this.good_quality = "";
      this.page = 1;
      this.getListData();
    },
    currentChange(val) {
      this.page = val;
      this.getListData();
    },
    closeModal() {
      this.$nextTick(() => {
        this.$refs["ruleForm"].clearValidate();
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.batch-list {
  padding: 40px;
  .search-box {
    display: flex;
    .el-button {
      height: 40px;
    }
    .el-input {
      width: 190px;
      margin-right: 15px;
    }

    .search-box-label {
      display: flex;
      align-items: center;
      box-sizing: border-box;
      font-size: 14px;
      margin-bottom: 20px;
      color: #333;
      span {
        // min-width: 50px;
        // text-align: right;
        margin-right: 6px;
        display: inline-block;
      }
    }
  }
  .sear_btns {
    text-align: right;
    margin-bottom: 20px;
    margin-right: 20px;
  }
  .add_batch {
    text-align: right;
    margin-bottom: 30px;
  }
}
</style>
